package com.pwt.scheduled;

import com.pwt.entity.*;
import com.pwt.model.CloudMessageModel;
import com.pwt.repository.*;
import com.pwt.utils.CloudHomeUtils;
import com.pwt.utils.MathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Service
public class ZzWatchChartsThread {

    @Autowired
    private CloudHomeScheduled cloudHomeScheduled;

    @Autowired
    private MERKMALCONTROLRepository merkmalcontrolRepository;

    @Autowired
    private ZzWatchChartsRepository zzWatchChartsRepository;

    @Autowired
    private JobNoRepository jobNoRepository;

    @Autowired
    private CloudHomeUtils cloudHomeUtils;

    @Autowired
    private LogCloudRepository logCloudRepository;

    @Autowired
    private WERTEVARRepository wertevarRepository;

    @Async("asyncServerExecutor")
    public void run(List<WERTEVAR> list){
        List<ZzWatchCharts> listZz = new ArrayList<>();
        for (WERTEVAR wertevar:list){
            ZzWatchCharts zzWatchCharts = zzWatchChartsRepository.queryByK0010AndK0014AndK0004(wertevar.getWVMASCHINE(),wertevar.getWV0014(),wertevar.getWVDATZEIT());
            MERKMALCONTROL merkmalcontrol = merkmalcontrolRepository.queryMERKMALCONTROLByMETEILAndMEMERKMAL(wertevar.getWVTEIL(),wertevar.getWVMERkMAL());
            Double previous = this.wertevarRepository.findByWVTEILAndWVMERkMALAndWVWERTNR(wertevar.getWVTEIL(),wertevar.getWVMERkMAL(),wertevar.getWVWERTNR()-1);
            CloudMessageModel cm = this.wertevarRepository.queryCloudHomeMessage(wertevar.getWVTEIL(),wertevar.getWVMERkMAL(),wertevar.getWVWERTNR());
            String state = "NOK";
            String message = "控制限";
            Integer type = 1;
            if(cm.getK0001() != null && merkmalcontrol != null && previous != null){
                Double abs = Math.abs(cm.getK0001()-previous);
                if(cm.getK0001() <= merkmalcontrol.getMEOGWCONTROL() && cm.getK0001() >= merkmalcontrol.getMEUGWCONTROL()
                && abs <= merkmalcontrol.getRMEOGWCONTROL() && abs >= merkmalcontrol.getRMEUGWCONTROL()){
                    state = "OK";
                }
            }else if(state.equals("NOK") && cm.getK0001() <= cm.getK2110() && cm.getK0001() >= cm.getK2111()){
                state = "SOMEOK";
                message = "控制限";
                type = 1;
            }else if(cm.getK0001() > cm.getK2110()*1.2 && cm.getK0001() < cm.getK2111()*1.2){
                state = "BAD";
                message = "报废限";
                type = 3;
            }else{
                message = "公差限";
                type = 2;
            }
            if(zzWatchCharts == null){
                zzWatchCharts = new ZzWatchCharts();
            }
            zzWatchCharts.setK0010(wertevar.getWVMASCHINE());
            zzWatchCharts.setK0014(wertevar.getWV0014());
            zzWatchCharts.setK0004(wertevar.getWVDATZEIT());
            zzWatchCharts.setRIGHTSTATUS(state);
            zzWatchCharts.setK1000(wertevar.getWVTEIL());
            zzWatchCharts.setK1002(this.zzWatchChartsRepository.queryK1002(wertevar.getWVTEIL()));
            listZz.add(zzWatchCharts);
            String msg = "零件号：'"+cm.getK0014()+"'"+message+"报错！，机器："+cm.getK0010()+"，测量类型：" +cm.getK2002()+"测量时间："+cm.getK0004()+",测量值："+cm.getK0001();
            this.sendCloudMessage(type,msg);
        }
        this.zzWatchChartsRepository.saveAllAndFlush(listZz);
    }


    /**
     * 云之家消息推送
     * @param type 1-控制限,2-公差限,3报废限
     * @param msg 2-消息内容
     */
    public void sendCloudMessage(Integer type,String msg){
        JobNo jobNo = this.jobNoRepository.queryJobNoByType(type);
        if(jobNo == null || jobNo.getJobNos() == null || "".equals(jobNo.getJobNos())) return;
        List<String> jobNos = Arrays.asList(jobNo.getJobNos().split(","));
        //前端配置消息
        msg= msg+jobNo.getMessage();
        //cloudHomeUtils.sendMessage(jobNos,msg);
        LogCloud cloud= new LogCloud();
        cloud.setJobNos(jobNo.getJobNos());
        cloud.setType(type);
        cloud.setMessage(msg);
        logCloudRepository.save(cloud);
    }

}
